unit NodeProperties;

interface

uses
  Winapi.Windows, Winapi.Messages,
  System.Variants, System.Classes, System.SysUtils,
  Vcl.StdCtrls, rTrack, Vcl.Menus, Vcl.Graphics, Vcl.Forms,
  Vcl.ExtCtrls, Vcl.Dialogs, Vcl.Controls, Vcl.Buttons;

type
  TNodePropsForm = class(TForm)
    Label4: TLabel;
    RoadOffsetEdit: TEdit;
    Label14: TLabel;
    RoadWidthEdit: TEdit;
    Label17: TLabel;
    RoadDetailEdit: TEdit;
    Menu: TPopupMenu;
    Applytoallnodes1: TMenuItem;
    Label3: TLabel;
    RoadMatNameButton: TSpeedButton;
    RoadMatnameEdit: TEdit;
    Label2: TLabel;
    BorderSizeEdit: TEdit;
    Label1: TLabel;
    BankingEdit: TEdit;
    procedure RoadMatNameButtonClick(Sender: TObject);
    procedure RoadWidthEditChange(Sender: TObject);
    procedure RoadMatnameEditChange(Sender: TObject);
    procedure Applytoallnodes1Click(Sender: TObject);
    procedure RoadDetailEditMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure RoadDetailEditChange(Sender: TObject);
    procedure RoadDetailEditKeyPress(Sender: TObject; var Key: Char);
    procedure FormShow(Sender: TObject);
    procedure RoadOffsetEditChange(Sender: TObject);
    procedure RoadOffsetEditKeyPress(Sender: TObject; var Key: Char);
    procedure BorderSizeEditChange(Sender: TObject);
    procedure BankingEditChange(Sender: TObject);
    procedure BankingEditKeyPress(Sender: TObject; var Key: Char);
  private
     
  public
     
    ScrollActive: Boolean;
  end;

var
  NodePropsForm: TNodePropsForm;

//---------------------------------------------------------------------------
implementation
//---------------------------------------------------------------------------

uses
  MatSelect, main;

{$R *.dfm}

procedure TNodePropsForm.RoadDetailEditChange(Sender: TObject);
var
  i: Integer;
begin
  if Sender = nil then
    for i := 0 to form1.track.count - 1 do
      form1.track.Nodes[i].road.Detail := Strtofloat(RoadDetailEdit.text)
  else
    form1.track.selected.road.Detail := Strtofloat(RoadDetailEdit.text);
end;

procedure TNodePropsForm.RoadDetailEditKeyPress(Sender: TObject; var Key: Char);
begin
  form1.diveditKeyPress(Sender, Key);
end;

procedure TNodePropsForm.RoadDetailEditMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  pt: TPoint;
begin
  if Button = mbRight then
  begin
    getcursorpos(pt);
    Menu.Popup(pt.X, pt.Y);
    Menu.Tag := Integer(Sender);
  end;
end;

procedure TNodePropsForm.RoadMatnameEditChange(Sender: TObject);
var
  i: Integer;
begin
  if RoadMatnameEdit.text = '' then
    RoadMatnameEdit.text := 'null';
  if Sender = nil then
    for i := 0 to form1.track.count - 1 do
      form1.track.Nodes[i].road.matname := RoadMatnameEdit.text
  else
    form1.track.selected.road.matname := RoadMatnameEdit.text;
end;

procedure TNodePropsForm.FormShow(Sender: TObject);
begin
  RoadOffsetEdit.text := format('%.4f', [form1.track.selected.road.YOffset]);
  RoadDetailEdit.text := format('%.4f', [form1.track.selected.road.Detail]);
  RoadWidthEdit.text := format('%.4f', [form1.track.selected.road.width]);
  RoadMatnameEdit.text := form1.track.selected.road.matname;
  BorderSizeEdit.text := format('%.4f', [form1.track.selected.BorderSize]);
  BankingEdit.text := format('%.4f', [form1.track.selected.Banking]);

end;

procedure TNodePropsForm.RoadOffsetEditChange(Sender: TObject);
var
  i: Integer;
begin
  if Sender = nil then
    for i := 0 to form1.track.count - 1 do
    begin
      form1.track.Nodes[i].road.YOffset := Strtofloat(RoadOffsetEdit.text);
      form1.track.Nodes[i].cube.Position.Y := Strtofloat(RoadOffsetEdit.text);
    end
  else
  begin
    form1.track.selected.road.YOffset := Strtofloat(RoadOffsetEdit.text);
    form1.track.selected.cube.Position.Y := Strtofloat(RoadOffsetEdit.text);
  end;
  form1.track.StructureChanged;
end;

procedure TNodePropsForm.RoadOffsetEditKeyPress(Sender: TObject; var Key: Char);
var
  len: Integer;
  text: String;
procedure DoBounds(text: String);
begin
  if (Strtofloat(text) < 0) then
  begin
    TEdit(Sender).text := '0';
    TEdit(Sender).SelStart := 1;
    Key := #0;
  end;
  if (Strtofloat(text) > 200) then
  begin
    TEdit(Sender).text := '200';
    TEdit(Sender).SelStart := length(text);
    Key := #0;
  end;
end;

begin

  if (pos(Key, '.0123456789'#8) = 0) then
    Key := #0
  else if Key = #8 then
  begin
    if (TEdit(Sender).sellength) > 0 then
      len := (TEdit(Sender).sellength) - 1
    else
      len := 0;

    if length(TEdit(Sender).text) - len <= 1 then
    begin
      TEdit(Sender).text := '0';
      TEdit(Sender).SelStart := 0;
      TEdit(Sender).sellength := 1;
      Key := #0;
    end
    else
    begin
      text := trim(copy(TEdit(Sender).text, 1, TEdit(Sender).SelStart) +
        copy(TEdit(Sender).text, TEdit(Sender).SelStart + TEdit(Sender)
        .sellength + 1, length(TEdit(Sender).text)));
      DoBounds(text);
    end;

  end
  else
  begin
    if (Key = '.') AND (pos('.', TEdit(Sender).text) > 0) then
    begin
      Key := #0;
      exit;
    end;

    text := trim(copy(TEdit(Sender).text, 1, TEdit(Sender).SelStart) + Key +
      copy(TEdit(Sender).text, TEdit(Sender).SelStart + TEdit(Sender).sellength
      + 1, length(TEdit(Sender).text)));
    if length(text) > 0 then
      DoBounds(text);
  end;

end;

procedure TNodePropsForm.RoadWidthEditChange(Sender: TObject);
var
  i: Integer;
begin
  if Sender = nil then
    for i := 0 to form1.track.count - 1 do
      form1.track.Nodes[i].road.width := Strtofloat(RoadWidthEdit.text)
  else
    form1.track.selected.road.width := Strtofloat(RoadWidthEdit.text);
end;

procedure TNodePropsForm.RoadMatNameButtonClick(Sender: TObject);
begin
  if matselectform.ShowModal = mrOK then
    if matselectform.ListBox1.Itemindex > -1 then
      RoadMatnameEdit.text := matselectform.ListBox1.Items
        [matselectform.ListBox1.Itemindex]
    else
      RoadMatnameEdit.text := '';
end;

procedure TNodePropsForm.Applytoallnodes1Click(Sender: TObject);
begin
  if TObject(Menu.Tag) IS TEdit then
  begin
    if assigned(TEdit(Menu.Tag).OnChange) then
      TEdit(Menu.Tag).OnChange(NIL);
  end
  else if TObject(Menu.Tag) IS TCombobox then
    if assigned(TCombobox(Menu.Tag).OnChange) then
      TCombobox(Menu.Tag).OnChange(NIL);
end;

procedure TNodePropsForm.BorderSizeEditChange(Sender: TObject);
var
  i: Integer;
begin
  if Sender = NIL then
    for i := 0 to form1.track.count - 1 do
    begin
      form1.track.Nodes[i].BorderSize := Strtofloat(BorderSizeEdit.text);
    end
  else
  begin
    form1.track.selected.BorderSize := Strtofloat(BorderSizeEdit.text);
  end;
end;

procedure TNodePropsForm.BankingEditChange(Sender: TObject);
var
  i: Integer;
begin
  if (BankingEdit.text = '-') or (BankingEdit.text = '') or
    (BankingEdit.text = '.') then
    exit;
  if Sender = nil then
  begin
    for i := 0 to form1.track.count - 1 do
      form1.track.Nodes[i].Banking := Strtofloat(BankingEdit.text);
  end
  else
    form1.track.selected.Banking := Strtofloat(BankingEdit.text);
end;

procedure TNodePropsForm.BankingEditKeyPress(Sender: TObject; var Key: Char);
var
  len: Integer;
  text: String;
procedure DoBounds(text: String);
begin
  if (text = '-') or (text = '') or (text = '.') then
    exit;
  if (Strtofloat(text) < -45) then
  begin
    TEdit(Sender).text := '-45';
    TEdit(Sender).SelStart := 1;
    Key := #0;
  end;
  if (Strtofloat(text) > 45) then
  begin
    TEdit(Sender).text := '45';
    TEdit(Sender).SelStart := length(text);
    Key := #0;
  end;
end;

begin

  if (pos(Key, '-.0123456789'#8) = 0) then
    Key := #0
  else if Key = #8 then
  begin
    if (TEdit(Sender).sellength) > 0 then
      len := (TEdit(Sender).sellength) - 1
    else
      len := 0;

    if length(TEdit(Sender).text) - len <= 1 then
    begin
      TEdit(Sender).text := '0';
      TEdit(Sender).SelStart := 0;
      TEdit(Sender).sellength := 1;
      Key := #0;
    end
    else
    begin
      text := trim(copy(TEdit(Sender).text, 1, TEdit(Sender).SelStart) +
        copy(TEdit(Sender).text, TEdit(Sender).SelStart + TEdit(Sender)
        .sellength + 1, length(TEdit(Sender).text)));
      DoBounds(text);
    end;

  end
  else
  begin
    if (Key = '.') AND (pos('.', TEdit(Sender).text) > 0) then
    begin
      Key := #0;
      exit;
    end;

    if (Key = '-') AND (pos('-', TEdit(Sender).text) > 0) then
    begin
      Key := #0;
      exit;
    end;

    text := trim(copy(TEdit(Sender).text, 1, TEdit(Sender).SelStart) + Key +
      copy(TEdit(Sender).text, TEdit(Sender).SelStart + TEdit(Sender).sellength
      + 1, length(TEdit(Sender).text)));
    if length(text) > 0 then
      DoBounds(text);
  end;

end;

end.
